package com.mall.service;

import java.util.List;

import com.mall.ibatis.bean.Frame;
import com.mall.ibatis.bean.FrameEx;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.ibatis.persistence.DaoConfig;
import com.mall.ibatis.persistence.iface.FrameexDAO;
import com.ibatis.dao.client.DaoManager;

public class FrameexService {
	private static final FrameexService instance = new FrameexService();
	private DaoManager daoManager = DaoConfig.getDaomanager();
	private FrameexDAO frameexDAO;

	public FrameexService(){
		frameexDAO = (FrameexDAO) daoManager.getDao(FrameexDAO.class);
	}

	public static FrameexService getInstance(){
		return instance;
	}
	
	//管理架构树的加载
	public List<Frame> frameexTreeLoading(String dir) {
		return frameexDAO.frameexTreeLoading(dir);
	}

	//查找节点数据
	public Frame selectFrameex(String frameexID) {
		return frameexDAO.selecFrameex(frameexID);
	}

	//节点修改保存
	public ReturnMessage saveFrameex(Frame frameex) {
		ReturnMessage returnMessage = null;
		try{
			int flag = frameexDAO.saveFrameex(frameex);
			if(flag>0){
				returnMessage = new ReturnMessage("1");
			}else{
				returnMessage = new ReturnMessage("0","更新失败,请检查更改的数据");
			}
		}catch (Exception e){
			returnMessage = new ReturnMessage("0","更新失败："+e.getMessage());
		}	
		return returnMessage; 
	}

	//节点删除
	public ReturnMessage deleteFrameex(Frame frameex) {
		ReturnMessage returnMessage = null;
		//通过传入的父节点id判断有没有子节点
		List<Frame> frameexs = frameexTreeLoading(frameex.getFrameID());
		if(frameexs.size() > 0){
			returnMessage = new ReturnMessage("0","删除失败："+"该结点下面还有子结点，请删除完子结点后再删除");
		}else {
			try{
				frameexDAO.deleteFrameex(frameex);
				returnMessage = new ReturnMessage("1");
			}catch (Exception e){
				returnMessage = new ReturnMessage("0","删除失败："+e.getMessage());
			}
		}
		
		
		
		return returnMessage; 
	}

	//新增子节点
	public ReturnMessage addSubFrameex(Frame frameex) {
		ReturnMessage returnMessage = null;
		try{
			//依靠返回新的id传递到页面上。帮组管理结构树动态生成节点
			String newFrameexId = frameexDAO.addSubFrameex(frameex);
			returnMessage = new ReturnMessage("1",newFrameexId);
		}catch (Exception e){
			returnMessage = new ReturnMessage("0","新增失败："+e.getMessage());
		}	
		return returnMessage;
	}
	
	
	public List<FrameEx> selectAllFrameEx() throws Exception
	{
		return frameexDAO.selectAllFrameEx();
	}

	public List<String> selectFrameexNameByPId(String subFrameexParentID) {

		return frameexDAO.selectFrameexNameByPId(subFrameexParentID);
	}
}
